/**
 * Created by Administrator on 2018-12-27.
 */
var widgetEchartUtil={
    opt_cfg_value: function (widgetPanelId) {
        var value = "{}"
        var url = basePath+"/xdWidgetEchart/getOne" ;
        $.ajax({
            dataType:"json",
            async:false,
            url:url,
            data: {widgetPanelId: widgetPanelId},
            success: function (rst) {
                if (rst.code == 0) {
                    value = rst.data
                }
            }
        })
        return value
    },
    initColorPicker:function(){//初始化颜色选择器
        //颜色div class='color-picker' _input='颜色input[name=?]'
        layui.use("colorpicker",function(){
            var colorpicker = layui.colorpicker;

            $('.color-picker').each(function(item,index){
                //var attr = $(item).attr()
                var inputName = $(this).attr("_input")
                //console.log(inputName)
                colorpicker.render({
                    elem: this
                    ,color: $('input[name="'+inputName+'"]').val()
                    ,done: function(color){
                        $('input[name="'+inputName+'"]').val(color);
                    }
                })
            })
        })
    },
    opt_cfg_event:function(panelId,form,laytpl){//图形系列配置：系列名名使用
        form.on('radio(opt_series_cfg_type)', function(data){
            //console.log(data.value)
            var value = data.value
            //fieldValue /fieldName
            widgetEchartUtil.opt_series_cfg_type_dom(panelId,value,laytpl,form)
        });

        //提交表单-系列配置
        form.on("submit(opt-series-cfg-form-bt)",function(obj){
            //console.log(obj)
            var valueJson = JSON.stringify(obj.field)
            $.post(basePath+"/xdWidgetEchart/save",{opt_series_cfg_type:valueJson,widgetPanelId:panelId,xmlNode:"opt_series_cfg_type"},function(rst){
                if(rst.code !=0){
                    layer.alert(rst.msg)
                }else{
                    layer.msg(rst.msg)
                }
            },'json');
            return false
        })

        //提交表单-option配置
        form.on("submit(opt-cfg-form-bt)",function(obj){
            //console.log(obj)
            var valueJson = JSON.stringify(obj.field)
            $.post(basePath+"/xdWidgetEchart/save",{tempoptionvalue:valueJson,widgetPanelId:panelId,xmlNode:"tempoptionvalue"},function(rst){
                if(rst.code !=0){
                    layer.alert(rst.msg)
                }else{
                    layer.msg(rst.msg)
                }
            },'json');
            return false
        })

        //新增系列-高级自定义
        form.on("submit(add-diyseries-bt)",function(obj){
            layer.prompt({title:'系列名称'},function(val, idx, ele){
                var seriesValue =  JSON.stringify({series_name:val})
                $.getJSON(basePath+"/xdWidgetEchart/update",
                    {widgetPanelId:panelId,xmlNode:"diyseries",seriesName:val,seriesValue:seriesValue,flag:'add'},
                    function(rst){
                        if(rst.code !=0){
                            layer.alert(rst.msg)
                        }else{
                            //console.log(rst.data)
                            widgetEchartUtil.freshSeries(rst.data)
                            layer.close(idx)
                        }
                    }
                )
            });
            return false
        })

        //删除系列-高级自定义
        form.on("submit(del-diyseries-bt)",function(obj){
            var elem = $(obj.elem)
            var seriesName = elem.parent().attr("id")
            layer.confirm('确定删除该系列配置吗?',{icon: 3,title:'系列名称'},function(idx){
                $.getJSON(basePath+"/xdWidgetEchart/update",
                    {widgetPanelId:panelId,xmlNode:"diyseries",seriesName:seriesName,seriesValue:null,flag:'del'},
                    function(rst){
                        if(rst.code !=0){
                            layer.alert(rst.msg)
                        }else{
                            widgetEchartUtil.freshSeries(rst.data)
                            layer.close(idx)
                        }
                    }
                )
            });
            return false
        })

        //删除-交互中 绑定的参数
        form.on("submit(del-action-bt)",function(obj){
            var elem = $(obj.elem)
            var pName = elem.parent().attr("id")
            layer.confirm('确定删除该参数吗?',{icon: 3,title:'删除绑定参数'},function(idx){
                $.getJSON(basePath+"/xdWidgetEchart/echartAction",{widgetPanelId:panelId,pName:pName,flag:"del",xmlNode:"action"}  ,function(rst){
                    if(rst.code !=0){
                        layer.alert(rst.msg)
                    }else{
                        widgetEchartUtil.freshAction(rst.data)
                        layer.close(layer.index)
                    }
                })
            });
            return false
        })

        //编辑系列-高级自定义
        form.on("submit(edit-diyseries-bt)",function(obj){
            var elem = $(obj.elem)
            var seriesName = elem.parent().attr("id")
            $.getJSON(basePath+"/xdWidgetEchart/getDiySeries",{widgetPanelId:panelId, seriesName:seriesName }, function(rst) {
                if (rst.code != 0) {
                    layer.alert(rst.msg)
                } else {
                    layer.open({
                        title:"<i class='layui-icon layui-icon-note'></i>&nbsp;&nbsp;"+seriesName,
                        type: 1,
                        area:["50%","85%"],
                        content: rst.formItem ,//这里content是一个普通的String
                        success: function(layero, index){
                            form.val("opt-series-form",rst.data)
                            widgetEchartUtil.initColorPicker()
                        }
                    });
                }
            })
            return false
        })

        //保存系列-配置
        form.on("submit(opt-series-form-bt)",function(obj){
            var valueJson = JSON.stringify(obj.field)
            var seriesName = $("input[name=series_name]").val()
            $.post(basePath+"/xdWidgetEchart/update",
                {widgetPanelId:panelId,xmlNode:"diyseries",seriesName:seriesName,seriesValue:valueJson,flag:'update' },
                function(rst){
                    if(rst.code !=0){
                        layer.alert(rst.msg)
                    }else{
                        layer.close(layer.index)
                    }
                },'json'
            )
            return false
        })

        //预览Echart图表
        form.on("submit(xdWidget-previewEchart-form-bt)",function(obj){
            window.open("./viewPanelEchartLast.html?id="+panelId,"预览")
            return false;
        })

        //保存发布Echart
        form.on("submit(xdWidget-saveEchart-form-bt)",function(obj){
            $.getJSON(basePath+"/xdWidgetEchart/edit",{widgetPanelId:panelId},function(rst){
                if(rst.code!=0){
                    layer.alert(rst.msg)
                }else{
                    layer.msg(rst.msg)
                }
            })
            return false;
        })

        //绑定参数
        form.on("submit(echartAction-form-bt)",function(obj){
            layer.open({
                title:"绑定参数",
                content:$("#echartAction-params-dom").html() ,
                type:1,
                area:['25%','50%'],
                success:function(layero, index){
                    form.render(null, 'echartAction-params-form');
                }
            })
            return false
        })

        //新增保存参数
        form.on("submit(echartAction-params-form-bt)",function(obj){
            var data = obj.field
            data.widgetPanelId = panelId
            data.xmlNode = "action"
            data.flag = 'add'
            ///{widgetPanelId:panelId,xmlNode:"diyseries",seriesName:seriesName,seriesValue:valueJson,flag:'update' }
            $.getJSON(basePath+"/xdWidgetEchart/echartAction",data  ,function(rst){
                if(rst.code !=0){
                    layer.alert(rst.msg)
                }else{
                    widgetEchartUtil.freshAction(rst.data)
                    layer.close(layer.index)
                }
            })
            return false
        })

    },
    //刷新系列配置
    freshSeries:function(seriesMap){
        $("div[opt-series-dom]").empty();
        $.each(seriesMap,function(key,value){
            $("div[opt-series-dom]").append('<span class="layui-badge-rim" id="'+key+'" >' +
                '<span class="layui-badge layui-bg-cyan" lay-submit lay-filter="edit-diyseries-bt" flag="edit" >'+key+'</span>' +
                '<span class="layui-badge layui-bg-gray" lay-submit lay-filter="del-diyseries-bt" flag="del">×</span></span>')
        })
        $(".layui-badge-rim").css({"margin-left":"3px","margin-right":"3px" })
    },

    //更新交互中绑定参数
    freshAction:function(actionMap){
        $("div[opt-action-dom]").html("<hr>")
        $.each(actionMap,function(key,value){
            $("div[opt-action-dom]").append('<span class="layui-badge-rim" id="'+key+'" >' +
                '<span class="layui-badge layui-bg-cyan"  >'+key+'</span>' +
                '<span class="layui-badge layui-bg-gray" lay-submit lay-filter="del-action-bt" flag="del">×</span></span>')
        })
        $(".layui-badge-rim").css({"margin-left":"3px","margin-right":"3px" })
    },

    //根据系列名类型，生成dom
    opt_series_cfg_type_dom:function(panelId,opt_series_cfg_type_value,laytpl,form){
        //根据opt_series_cfg_type值，初始化dom
        var targetDom = $("#opt_series_cfg_type_dom")
        var script = "#opt_series_cfg_type_script1"
        if(opt_series_cfg_type_value){
            if(opt_series_cfg_type_value !='fieldValue'){
                script = "#opt_series_cfg_type_script2"
            }
            $.ajax({
                dataType:"json",
                async:false,
                url:basePath+"/xdWidgetPanel/viewSQLFieldByPanel",
                data: {panelId:panelId},
                success: function (rst) {
                    if (rst.code == 0) {

                        laytpl($("#opt_series_cfg_type_weidu").html()).render(rst.data,function(htmlCode){
                            targetDom.html(htmlCode)
                        })

                        laytpl($(script).html()).render(rst.data,function(htmlCode){
                            targetDom.append(htmlCode)
                            form.render("select","opt-series-cfg-form")
                            form.render("checkbox","opt-series-cfg-form")
                            form.render("radio","opt-series-cfg-form")
                        })
                    }else{
                        layer.alert(rst.msg,{icon:2})
                    }
                }
            })
        }
    }
}